package com.boarsoft.dao.sharding.oceanbase.mybatis.inspector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

public class RouteParameters {
	// private static final String GROUP_ID = "GROUP_ID";
	// private static final String TABLE_NAME = "TABLE_NAME";
	// private static final String DB_INDEX = "DB_INDEX";
	// private static final String TABLE_ID = "TABLE_ID";
	// private static final String ELASTIC_ID = "ELASTIC_ID";
	// private static final String SHARDING_KEY = "SHARDING_KEY";

	private String groupId;
	private String targetTable;
	private String dbIndex;
	private String tableId;
	private String elasticId;
	private Map<String, Object> params = new HashMap<>();

	public RouteParameters() {
	}

	public void addParameter(String col, Object value) {
		this.params.put(col, value);
	}

	public String getGroupId() {
		return this.groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getTargetTable() {
		return this.targetTable;
	}

	public void setTargetTable(String targetTable) {
		this.targetTable = targetTable;
	}

	public void setTargetTables(List<String> targetTables) {
		if (targetTables != null && !targetTables.isEmpty()) {
			this.targetTable = (String) targetTables.get(0);
		} else {
			throw new IllegalArgumentException("targetTables must not be empty");
		}
	}

	public String getDbIndex() {
		return this.dbIndex;
	}

	public void setDbIndex(String dbIndex) {
		this.dbIndex = dbIndex;
	}

	public String getTableId() {
		return this.tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public String getElasticId() {
		return this.elasticId;
	}

	public void setElasticId(String elasticId) {
		this.elasticId = elasticId;
	}

	private String format(String prefix, String value) {
		return prefix + "(" + value + ")";
	}

	public String getParamsString0() {
		List<String> tokens = new ArrayList<>(5);
		if (this.groupId != null) {
			tokens.add(this.format("GROUP_ID", this.groupId));
		}

		if (this.targetTable != null) {
			tokens.add(this.format("TABLE_NAME", this.targetTable));
		}

		if (this.dbIndex != null) {
			tokens.add(this.format("DB_INDEX", this.dbIndex));
		}

		if (this.tableId != null) {
			tokens.add(this.format("TABLE_ID", this.tableId));
		}

		if (this.elasticId != null) {
			tokens.add(this.format("ELASTIC_ID", this.elasticId));
		}

		return StringUtils.join(tokens, ",");
	}

	@SuppressWarnings("unchecked")
	public String getParamsString1() {
		if (this.params.isEmpty()) {
			return "";
		} else {
			List<String> tokens = new ArrayList<>(this.params.size());
			Iterator<?> var2 = this.params.entrySet().iterator();

			while (var2.hasNext()) {
				Map.Entry<String, Object> entry = (Entry<String, Object>) var2.next();
				String key = (String) entry.getKey();
				Object value = entry.getValue();
				if (value instanceof Integer) {
					tokens.add(key + "=" + value);
				} else {
					if (!(value instanceof String)) {
						throw new IllegalArgumentException("Only support Integer and String value");
					}

					tokens.add(key + "='" + value + "'");
				}
			}

			return "SHARDING_KEY(" + StringUtils.join(tokens, ",") + ")";
		}
	}
}
